package com.ruoyi.project.purchase.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.information.domain.TbCustomers;
import com.ruoyi.project.information.mapper.TbCustomersMapper;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.domain.RyDepotHead;
import com.ruoyi.project.purchase.domain.RyPurchaseOrder;
import com.ruoyi.project.purchase.domain.pojo.*;
import com.ruoyi.project.purchase.mapper.RyDepotDetailMapper;
import com.ruoyi.project.purchase.mapper.RyDepotHeadMapper;
import com.ruoyi.project.purchase.mapper.RyPurchaseOrderMapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.warehouse.domain.RyDepot;
import com.ruoyi.project.warehouse.domain.RyDepotItem;
import com.ruoyi.project.warehouse.domain.RyInventoryCurrentStock;
import com.ruoyi.project.warehouse.domain.WareCurrentStock;
import com.ruoyi.project.warehouse.mapper.RyDepotItemMapper;
import com.ruoyi.project.warehouse.mapper.RyDepotMapper;
import com.ruoyi.project.warehouse.mapper.WareCurrentStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname infcController
 * @Description TODO
 * @Date 2022/9/23 18:04
 * @Created by You
 */

//@Anonymous
@RestController
@RequestMapping("/infcbck")
public class InfcControllerBak {
    @Autowired
    private RyDepotHeadMapper ryDepotHeadMapper;

    @Autowired
    private RyDepotDetailMapper ryDepotDetailMapper;

    @Autowired
    private RyDepotMapper ryDepotMapper;

    @Autowired
    private RyPurchaseOrderMapper ryPurchaseOrderMapper;


    @Autowired
    private RyDepotItemMapper ryDepotItemMapper;

    @Autowired
    private TbCustomersMapper tbCustomersMapper;

    @Autowired
    private WareCurrentStockMapper wareCurrentStockMapper;



    /**
     * --项目总清单
     * 新增项目总清单及项目总清单子表信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
        System.out.println("depotDetailBean---->" + depotHeadItem);
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //新增项目库(每一个新建项目都会对应生成一个项目库)
        RyDepot ryDepot = new RyDepot();
        ryDepot.setName(depotHeadItem.getProjectName());
        ryDepot.setType("2");//2代表项目库
        ryDepot.setSort("3");//排序默认3,1和2代表总库和工具库
        int res1 =  ryDepotMapper.insertRyDepot(ryDepot);
        if(res1 > 0){//新增项目库成功
            //新增主表数据
            RyDepotHead ryDepotHead = new RyDepotHead();
            ryDepotHead.setDepotId(ryDepot.getId());
            ryDepotHead.setProjectCode(depotHeadItem.getProjectCode());
            ryDepotHead.setProjectName(depotHeadItem.getProjectName());
            ryDepotHead.setProjectStatus(depotHeadItem.getProjectStatus());
            ryDepotHead.setNumber(depotHeadItem.getNumber());
            ryDepotHead.setRemark(depotHeadItem.getRemark());
            ryDepotHead.setFileName(depotHeadItem.getFileName());
            ryDepotHead.setCreateTime(DateUtils.getNowDate());
            ryDepotHead.setCreator(user.getUserId());
            ryDepotHead.setType("其他");
            ryDepotHead.setSubType("项目总清单");
//            ryDepotHead.setCreator(user.getUserId());
            int res2 = ryDepotHeadMapper.insertRyDepotHead(ryDepotHead);
            System.out.println("id---->" + ryDepotHead.getId());
            if(res2 > 0){
                //遍历明细数据，新增单据明细数据
                for(RyDepotItem ryDepotItem : tableData){
                    ryDepotItem.setHeaderId(ryDepotHead.getId());//设置刚生成的主表id
                    //设置入库仓库和出库仓库为刚生成的项目仓库信息
                    ryDepotItem.setDepotId(ryDepot.getId());
                    ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
                }
            }
        }

        return AjaxResult.success("新增成功");
    }

    /**
     * --项目总清单
     * 获取项目总清单详细信息
     */
    @GetMapping(value = "/selectByDepotId/{id}")
    public AjaxResult selectByDepotId(@PathVariable("id") Long id)
    {

        DepotHeadItem depotHeadItem = new DepotHeadItem();
        RyDepotHead ryDepotHead = ryDepotHeadMapper.selectRyDepotHeadById(id);
        //主表数据
        depotHeadItem.setId(ryDepotHead.getId());
        depotHeadItem.setProjectCode(ryDepotHead.getProjectCode());
        depotHeadItem.setProjectName(ryDepotHead.getProjectName());
        depotHeadItem.setDepotId(ryDepotHead.getDepotId());
        depotHeadItem.setProjectStatus(ryDepotHead.getProjectStatus());
        depotHeadItem.setNumber(ryDepotHead.getNumber());
        depotHeadItem.setContractNumber(ryDepotHead.getContractNumber());
        depotHeadItem.setCreator(ryDepotHead.getCreator());
        depotHeadItem.setOperTime(ryDepotHead.getOperTime());
        depotHeadItem.setFileName(ryDepotHead.getFileName());
        depotHeadItem.setSalesMan(ryDepotHead.getSalesMan());
        depotHeadItem.setCreateTime(ryDepotHead.getCreateTime());
        depotHeadItem.setRemark(ryDepotHead.getRemark());
        //子表数据
        List<RyDepotItem> tableData = ryDepotItemMapper.selectRyDepotItemByDepotId(id);
        depotHeadItem.setTableData(tableData);
        return AjaxResult.success(depotHeadItem);
    }


    /**
     * --项目总清单
     * 修改项目总清单
     */
    @RequestMapping("/editDepotHeadAndDetail")
    public AjaxResult editDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
        System.out.println("depotDetailBean---->" + depotHeadItem);
        //获取用户信息
//        SysUser user = SecurityUtils.getLoginUser().getUser();

        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //修改主表数据
        RyDepotHead ryDepotHead = new RyDepotHead();
        ryDepotHead.setId(depotHeadItem.getId());//主键，用于定位修改数据
        ryDepotHead.setProjectCode(depotHeadItem.getProjectCode());
        ryDepotHead.setProjectName(depotHeadItem.getProjectName());
        ryDepotHead.setProjectStatus(depotHeadItem.getProjectStatus());
        ryDepotHead.setNumber(depotHeadItem.getNumber());
        ryDepotHead.setOperTime(DateUtils.getNowDate());
        ryDepotHead.setFileName(depotHeadItem.getFileName());
        ryDepotHead.setUpdateTime(DateUtils.getNowDate());
        ryDepotHead.setRemark(depotHeadItem.getRemark());
        ryDepotHeadMapper.updateRyDepotHead(ryDepotHead);

        //删除单据的明细(原因是修改时也有可能新增明细数据，这些新增明细数据是没有id的)
        ryDepotItemMapper.deleteDepotItemByHeadId(ryDepotHead.getId());

        //遍历明细数据，新增单据明细数据
        for(RyDepotItem ryDepotItem : tableData){
            ryDepotItem.setHeaderId(ryDepotHead.getId());//设置刚生成的主表id
            //设置入库仓库和出库仓库为刚生成的项目仓库信息
            ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
        }

        return AjaxResult.success("修改成功");
    }


    /**
     * 查询可以生成订单的项目明细数据
     */
    @GetMapping("/selectDetailForOrder")
    public AjaxResult selectDetailForOrder(ParamBean paramBean){
        if(paramBean.getLinkHeadId() != null){//项目总清单id
            paramBean.setHeaderId(paramBean.getLinkHeadId());
        }
        if(paramBean.getOrganId() != null){//供应商
            //查询供应商信息
            TbCustomers tbCustomers = tbCustomersMapper.selectTbCustomersById(paramBean.getOrganId());
            paramBean.setSupply(tbCustomers.getCompanyName());
        }
        List<RyDepotItem> resList = ryDepotItemMapper.selectItemForOrder(paramBean);
        for(RyDepotItem ryDepotItem : resList){
            //根据物品id和总仓库id查询总库库物品当前库存量以及相关信息(总库id为1)
            RyInventoryCurrentStock ryInventoryCurrentStock = new RyInventoryCurrentStock();
            ryInventoryCurrentStock.setInventoryId(ryDepotItem.getInventoryId());//物品id
            ryInventoryCurrentStock.setDepotId(1L);//仓库id
            WareCurrentStock wareCurrentStock = wareCurrentStockMapper.getInfoByDepotInventoryId(ryInventoryCurrentStock);
            if(wareCurrentStock == null){
                ryDepotItem.setUsableNum(Double.valueOf(0));//总库可用数量
            } else {
                ryDepotItem.setUsableNum(wareCurrentStock.getCurrentNumber());//总库可用数量
            }
        }

        return AjaxResult.success(resList);
    }


}
